package com.ymatou.iapi.withdraw.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.withdraw.parameter.PassApprovalBean;
import com.ymatou.iapi.withdraw.parameter.QueryAvailableTimesBean;
import com.ymatou.iapi.withdraw.parameter.applyAlipayWithDrawForSellerBean;
import com.ymatou.iapi.withdraw.parameter.applyBankWithDrawForBuyerBean;
import com.ymatou.iapi.withdraw.parameter.applyBankWithDrawForSellerBean;
import com.ymatou.iapi.withdraw.service.PassApprovalCall;
import com.ymatou.iapi.withdraw.service.QueryAvailableTimesCall;
import com.ymatou.iapi.withdraw.service.applyAlipayWithDrawForSellerCall;
import com.ymatou.iapi.withdraw.service.applyBankWithDrawForBuyerCall;
import com.ymatou.iapi.withdraw.service.applyBankWithDrawForSellerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.database.model.withdraw.Ymt_InvoiceFundFlowLogExample;
import com.ymttest.database.model.withdraw.Ymt_InvoiceOperationLogExample;
import com.ymttest.database.model.withdraw.Ymt_InvoiceSerialSeedExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawCounterExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawIdempotentExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoiceExample;
import com.ymttest.database.model.withdraw.Ymt_WithDrawInvoiceXloboLogExample;
import com.ymttest.database.sqlwapper.WithdrawWapper;
import com.ymttest.utils.TestDataManager;
/**
*可用提现次数查询--买家查询
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryAvailableTimes{
	private static QueryAvailableTimesBean bean;
	private static QueryAvailableTimesCall call;
	Ymt_WithDrawInvoiceExample ymtinvoive=new Ymt_WithDrawInvoiceExample();//提现申请信息主表
	Ymt_InvoiceSerialSeedExample ymtserialseed=new Ymt_InvoiceSerialSeedExample();//单据编号种子表
	Ymt_WithDrawInvoiceXloboLogExample ymtxlobolog=new Ymt_WithDrawInvoiceXloboLogExample();//提现到贝海的日志
	Ymt_InvoiceFundFlowLogExample ymtflowlog=new Ymt_InvoiceFundFlowLogExample();//资金流转操作日志表
	Ymt_InvoiceOperationLogExample ymtoperationlog=new Ymt_InvoiceOperationLogExample();//单据操作日志表
	Ymt_WithDrawIdempotentExample ymtidempotent=new Ymt_WithDrawIdempotentExample();//提现申请幂等键记录表
	Ymt_WithDrawCounterExample ymtcounter=new Ymt_WithDrawCounterExample();//安利会提现技术
	
	applyAlipayWithDrawForSellerBean bean2=new applyAlipayWithDrawForSellerBean();
	applyAlipayWithDrawForSellerCall call2=new applyAlipayWithDrawForSellerCall();
	
	WithdrawWapper WithdrawWapper=new WithdrawWapper();
	
	private final int sellerid=3383;
	private final String sellerName="wesper";
	
	private final int userId=4085;
	private final String userName="lunchzhao";

	private final String fundAccountId_sellerId="925CF663-FCBA-4380-846B-32162E58C345";
	private final String fundAccountName_sellerId="吴文";
	private final String fundAccountNo_sellerId="123456789123";
	
	private final String fundAccountId_buer="925CF663-FCBA-4380-846B-32162E58C317";
	private final String fundAccountName_buer="吴文买家";
	private final String fundAccountNo_buer="987654321789";
	
	private final String abroadFundAccountId="a0d8378e-af28-45bc-b211-5304d4869829";
	private final String abroadFundAccountName="wuwen";
	private final String abroadFundAccountNo="123456789123456";
	
	private final String alipayAccountNo_seller="18621614678";
	private final String alipayAccountName_seller="wuwenseller";
	private final String alipayAccountName_Id_seller="9d53eb3a-5867-4334-9735-269156eb7a26";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("可用提现次数查询--买家查询");
	}

	@Before
	public void caseUp() {
		bean=new QueryAvailableTimesBean();
		call=new QueryAvailableTimesCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	/**
	 * 买手提现到支付宝
	 * @return
	 * @throws Exception
	 */
	private String applybankForBuyer(int accounType) throws Exception {
		applyBankWithDrawForBuyerBean bean=new applyBankWithDrawForBuyerBean();
		applyBankWithDrawForBuyerCall call=new applyBankWithDrawForBuyerCall();
		//给帐号充值
		String accountid = AccountCallServiceV2.getAccountId(userId, accounType);
		AccountCallServiceV2.resetAccount(accountid, new BigDecimal(999999),
				new BigDecimal(0));
		//冻结资金
		BigDecimal freeze= WithdrawWapper.getSumFreezebyUserId(userId);
		Logger.comment("################冻结资金："+freeze);
		//查看账户余额
		BigDecimal balance=AccountCallServiceV2.getAccountBalance(userId, accounType);
		Logger.comment("################账户余额："+balance);
		
		bean.setUserId(userId);
		bean.setWithDrawChannel(0);////用户类型(0买家，１买手）
		bean.setAccountTypeId(accounType);//提现帐户类型Id，默认值:1.人民币余额帐户 5安利会
	    bean.setWithDrawAmount(new BigDecimal(10));
	    bean.setBusinessNo(new TestDataManager().getRandomNum(8));
        bean.setRemark("测试提现到国际银行卡");
        bean.setApplyUserId(userId);
        bean.setApplyLoginId(userName);
        
        bean.setBankAccountName(fundAccountName_buer);
        bean.setBankAccountNo(fundAccountNo_buer);
        bean.setFundAccountId(fundAccountId_buer);
	    bean.setCurrencyType(1);//1 人名币
        bean.setIdempotentNo(new TestDataManager().getRandomNum(8));
        bean.setWithDrawChannel(3);
		call.setData(bean);
		call.callService();		
		return call.getInvoiceId();
    }
	/**
	 * 买手提现到支付宝
	 * @return
	 * @throws Exception
	 */
	private String applyAlipayForBuyer(int accounType) throws Exception {
		applyBankWithDrawForBuyerBean bean=new applyBankWithDrawForBuyerBean();
		applyBankWithDrawForBuyerCall call=new applyBankWithDrawForBuyerCall();
		//给帐号充值
		String accountid = AccountCallServiceV2.getAccountId(userId, 5);
		AccountCallServiceV2.resetAccount(accountid, new BigDecimal(999999),
				new BigDecimal(0));
		//冻结资金
		BigDecimal freeze= WithdrawWapper.getSumFreezebyUserId(userId);
		Logger.comment("################冻结资金："+freeze);
		//查看账户余额
		BigDecimal balance=AccountCallServiceV2.getAccountBalance(userId, 5);
		Logger.comment("################账户余额："+balance);
		
		bean.setUserId(userId);
		bean.setWithDrawChannel(0);////用户类型(0买家，１买手）
		bean.setAccountTypeId(5);//提现帐户类型Id，默认值:1.人民币余额帐户 5安利会
	    bean.setWithDrawAmount(new BigDecimal(10));
	    bean.setBusinessNo(new TestDataManager().getRandomNum(8));
        bean.setRemark("测试提现到国际银行卡");
        bean.setApplyUserId(userId);
        bean.setApplyLoginId(userName);
        
        bean.setBankAccountName(fundAccountName_buer);
        bean.setBankAccountNo(fundAccountNo_buer);
        bean.setFundAccountId(fundAccountId_buer);
	    bean.setCurrencyType(1);//1 人名币
        bean.setIdempotentNo(new TestDataManager().getRandomNum(8));
        bean.setWithDrawChannel(3);
		call.setData(bean);
		call.callService();		
		return call.getInvoiceId();
    }
	
	private void checkAmount() {
		//可提现最大金额，提现大于500，就最大500，小于500，就是余额的20%，也就是最大只能提现500
		//可提现最小金额固定10
		//获取用户余额
		BigDecimal balance=AccountCallServiceV2.getAccountBalance(bean.getUserId(), bean.getAccountTypeId());
		
		Logger.verifyEquals(10d, call.getMinAmount(), "验证最小提现金额minAmount");
		if(call.getAvailableTimes().equals("0"))
		  Logger.verifyEquals(0d, call.getMaxAmount(), "验证最大提现金额maxAmount");
		else {
			
			BigDecimal amount=balance.multiply(new BigDecimal(0.2)).stripTrailingZeros();
			if(amount.compareTo(new BigDecimal("500"))>=0)
			   Logger.verifyEquals(500d, call.getMaxAmount(), "验证最大提现金额maxAmount");
			else {
				Logger.verifyEquals(amount.toPlainString()
						,new BigDecimal( call.getMaxAmount()).stripTrailingZeros().toPlainString(), "验证最大提现金额maxAmount");
			}
		}
		Logger.verifyEquals(balance.stripTrailingZeros().toPlainString()
				,new BigDecimal( call.getAvailAmount()).stripTrailingZeros().toPlainString(), "验证最大提现金额availAmount");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAvailableTimes_001() {
		Logger.start(true,"正例-可用提现次数查询-提现转出帐户类型,1.人民币余额帐户-可提现的次数(账户类型等于1，不计数，所以不会出现没有可提现次数)");
		try {
			
			applybankForBuyer(1);
			bean.setUserId(userId);
			bean.setAccountTypeId(1);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("true", call.getSuccess(), "验证success");
			Logger.verifyEquals("200", call.getCode(), "验证code");
			Logger.verifyEquals("1", call.getLimitTimes(), "验证LimitTimes");
			Logger.verifyEquals("3", call.getLimitCycle(), "验证LimitCycle(1年,2月,3日,4周)");
			Logger.verifyEquals("1", call.getAvailableTimes(), "验证AvailableTimes");
			
			String balance=AccountCallServiceV2.getAccountBalance(bean.getUserId(), bean.getAccountTypeId()).stripTrailingZeros().toPlainString();
			Logger.verifyEquals(10d, call.getMinAmount(), "验证最小提现金额minAmount");
			Logger.verifyEquals(balance
					,new BigDecimal( call.getMaxAmount()).stripTrailingZeros().toPlainString(), "验证最大提现金额maxAmount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAvailableTimes_001_1() {
		Logger.start(true,"正例-可用提现次数查询-提现转出帐户类型,1.人民币余额帐户-有可提现的次数");
		try {
			int userId1=TestDataManager.getRandomIntNum(5);
			//给帐号充值
			String accountid = AccountCallServiceV2.getAccountId(userId1, 1);
			AccountCallServiceV2.resetAccount(accountid, new BigDecimal(999999),
					new BigDecimal(0));

			bean.setUserId(userId1);
			bean.setAccountTypeId(1);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证success");
			Logger.verifyEquals("200", call.getCode(), "验证code");
			Logger.verifyEquals("1", call.getLimitTimes(), "验证LimitTimes");
			Logger.verifyEquals("3", call.getLimitCycle(), "验证LimitCycle(1年,2月,3日,4周)");
			Logger.verifyEquals("1", call.getAvailableTimes(), "验证AvailableTimes");
			
			String balance=AccountCallServiceV2.getAccountBalance(bean.getUserId(), bean.getAccountTypeId()).stripTrailingZeros().toPlainString();
			Logger.verifyEquals(10d, call.getMinAmount(), "验证最小提现金额minAmount");
			Logger.verifyEquals(balance
					,new BigDecimal( call.getMaxAmount()).stripTrailingZeros().toPlainString(), "验证最大提现金额maxAmount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAvailableTimes_001_2() {
		Logger.start(true,"正例-可用提现次数查询-提现转出帐户类型,5.安利会员分红帐户-没有可提现的次数");
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			LocalDateTime l=LocalDateTime.now();
			String begin=l.getYear()+"-"+l.getMonthValue()+"-"+l.getDayOfMonth()+" 00:00:00";
			ymtcounter.createCriteria().andCounterNameEqualTo("withdrawtimes").andTargetIdEqualTo("User"+userId+"_Type5")
			.andCycleTimeEqualTo(sf.parse(begin));
			
			int count=WithdrawWapper.selectYmtWithDrawCounter(ymtcounter).size();
			if(count==0)
		      applybankForBuyer(5);
			bean.setUserId(userId);
			bean.setAccountTypeId(5);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证success");
			Logger.verifyEquals("200", call.getCode(), "验证code");
			Logger.verifyEquals("1", call.getLimitTimes(), "验证LimitTimes");
			Logger.verifyEquals("3", call.getLimitCycle(), "验证LimitCycle(1年,2月,3日,4周)");
			Logger.verifyEquals("0", call.getAvailableTimes(), "验证AvailableTimes");
			
		    this.checkAmount();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAvailableTimes_001_3() {
		Logger.start(true,"正例-可用提现次数查询-提现转出帐户类型,5.安利会员分红帐户-有可提现的次数");
		try {
			int userId=TestDataManager.getRandomIntNum(5);
			//给帐号充值
			String accountid = AccountCallServiceV2.getAccountId(userId, 5);
			AccountCallServiceV2.resetAccount(accountid, new BigDecimal(999999),
					new BigDecimal(0));
			bean.setUserId(userId);
			bean.setAccountTypeId(5);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证success");
			Logger.verifyEquals("200", call.getCode(), "验证code");
			Logger.verifyEquals("1", call.getLimitTimes(), "验证LimitTimes");
			Logger.verifyEquals("3", call.getLimitCycle(), "验证LimitCycle(1年,2月,3日,4周)");
			Logger.verifyEquals("1", call.getAvailableTimes(), "验证AvailableTimes");
			checkAmount();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAvailableTimes_001_4() {
		Logger.start(true,"正例-可用提现次数查询-提现转出帐户类型,5.安利会员分红帐户-可提现的次数-支付宝");
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			LocalDateTime l=LocalDateTime.now();
			String begin=l.getYear()+"-"+l.getMonthValue()+"-"+l.getDayOfMonth()+" 00:00:00";
			ymtcounter.createCriteria().andCounterNameEqualTo("withdrawtimes").andTargetIdEqualTo("User"+userId+"_Type5")
			.andCycleTimeEqualTo(sf.parse(begin));

			int count=WithdrawWapper.selectYmtWithDrawCounter(ymtcounter).size();
			if(count==0)
		    	this.applyAlipayForBuyer(5);
			bean.setUserId(userId);
			bean.setAccountTypeId(5);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证success");
			Logger.verifyEquals("200", call.getCode(), "验证code");
			Logger.verifyEquals("1", call.getLimitTimes(), "验证LimitTimes");
			Logger.verifyEquals("3", call.getLimitCycle(), "验证LimitCycle(1年,2月,3日,4周)");
			Logger.verifyEquals("0", call.getAvailableTimes(), "验证AvailableTimes");
			checkAmount();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAvailableTimes_008() {
		Logger.start(true,"正例-可用提现次数查询-提现转出帐户类型,拒绝审核");
		try {
			//给帐号充值
			String accountid = AccountCallServiceV2.getAccountId(userId, 1);
			AccountCallServiceV2.resetAccount(accountid, new BigDecimal(999999),
					new BigDecimal(0));
			applybankForBuyer(1);
			bean.setUserId(userId);
			bean.setAccountTypeId(1);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("true", call.getSuccess(), "验证success");
			Logger.verifyEquals("200", call.getCode(), "验证code");
			Logger.verifyEquals("1", call.getLimitTimes(), "验证LimitTimes");
			Logger.verifyEquals("3", call.getLimitCycle(), "验证LimitCycle(1年,2月,3日,4周)");
			Logger.verifyEquals("1", call.getAvailableTimes(), "验证AvailableTimes");
			
			String balance=AccountCallServiceV2.getAccountBalance(bean.getUserId(), bean.getAccountTypeId()).stripTrailingZeros().toPlainString();
			Logger.verifyEquals(10d, call.getMinAmount(), "验证最小提现金额minAmount");
			Logger.verifyEquals(balance
					,new BigDecimal( call.getMaxAmount()).stripTrailingZeros().toPlainString(), "验证最大提现金额maxAmount");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_QueryAvailableTimes_002() {
		Logger.start(true,"正例-可用提现次数查询--未设置可提现次数(设置可提现次数是在discoufig中配置,此case无法验证)");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			call.setData(bean);
			call.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_QueryAvailableTimes_003() {
		Logger.start(true,"正例-可用提现次数查询--accountTypeId提现转出帐户类型不等于1或者5(研发这边不做处理)");
		try {
			
			bean.setUserId(userId);
			bean.setAccountTypeId(99999);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("true", call.getSuccess(), "验证success");
			Logger.verifyEquals("200", call.getCode(), "验证code");
			Logger.verifyEquals("1", call.getLimitTimes(), "验证LimitTimes");
			Logger.verifyEquals("2", call.getLimitCycle(), "验证LimitCycle(1年,2月,3日,4周)");
			Logger.verifyEquals("1", call.getAvailableTimes(), "验证AvailableTimes");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAvailableTimes_006() {
		Logger.start(true,"正例-可用提现次数查询--userId为空");
		try {
			
			//applybankForBuyer();
			//bean.setUserId(userId);
			bean.setAccountTypeId(1);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("false", call.getSuccess(), "验证success");
			Logger.verifyEquals("400", call.getCode(), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAvailableTimes_007() {
		Logger.start(true,"正例-可用提现次数查询--accountTypeId为空");
		try {
			//applybankForBuyer();
			bean.setUserId(userId);
			//bean.setAccountTypeId(1);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("false", call.getSuccess(), "验证success");
			Logger.verifyEquals("400", call.getCode(), "验证code");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}